#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include<QTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTimer* timer = new QTimer(this); //创建定时器对象

    timer->setTimerType(Qt::PreciseTimer);

    connect(ui->loop_button,&QPushButton::clicked,this,[=](){
        if(timer->isActive())
        {
            timer->stop();
            ui->loop_button->setText("开始");
        }
        else {
            ui->loop_button->setText("结束");
            timer->start(1000); //启动定时器  1000ms
        }

    });

    connect(timer,&QTimer::timeout,this,[=](){
        QTime time = QTime::currentTime();
        QString tmstr = time.toString("hh:mm:ss.zzz");
        ui->cur_time->setText(tmstr);
    });

    connect(ui->once_button,&QPushButton::clicked,this,[=](){
        //获取2s后的系统时间
        QTimer::singleShot(2000,this,[=](){
            QTime time = QTime::currentTime();
            QString tmstr = time.toString("hh:mm:ss.zzz");
            ui->once_time->setText(tmstr);
        });
    });



}

MainWindow::~MainWindow()
{
    delete ui;
}

